<<

IT452 Advanced Web and Internet Systems

Set 9: Web Services (Chapter 28, loosely) Web Services

• “any service available on the Web that has been designed for consumption by programs, independent of the being used” • Two primary camps – REST (sometimes just “HTTP”) • “Representational State Transfer” • Exchanging documents • Many HTTP actions

– SOAP • Exchanging messages, or RPC • Mainly HTTP POST REST

• Use all of HTTP for sensible document management and caching: – POST – make new document – HEAD – get doc metadata – GET – retrieve document (no state change) – PUT – update document – DELETE – delete document • Requests – all state info is in the URL • Response format?

• In practice – often GET for everything – Works in browser – But violates “no side effects” rule SOAP

• Originally “Simple Object Access Protocol” • Two views – 1. Exchanging messages – 2. Performing RPC • Request – Mostly POST (but need not be just HTTP!) – A complex XML document – What parameters/functions are legal??

• Response format: XML

REST Example 1

• Get the weather from wunderground.com

• http://www.wunderground.com/weather/api/d/documentation.html (Ex 1) Weather XML Data From: http://api.wunderground.com/api/XXX/conditions/forecast/q/21409.

Annapolis, MD Annapolis MD Maryland US US 21409 39.02930832 -76.43528748 6.00000000 KMDANNAP10 Last Updated on March 9, 3:08 PM EST Fri, 09 Mar 2012 15:08:35 -0500 1331323715 Fri, 09 Mar 2012 15:09:00 -0500 1331323740 EST America/New_York -0500 Clear 56.1 F (13.4 C) 56.1 (Ex 1) weather.

Web Services using XSLT

This is a webpage.

You have a lot of content on the page, and want to localize it for the user.

One easy way is to provide the weather!

Let's paste in your local weather using the wunderground.com , ask for a zipcode, and then use XSLT to transform the result into some nice XHTML.

We'll paste the result below.

Type your zip code:

This is where the transformed XML in XHTML form will appear.

(Ex 1) transform.js function getWeather() { var zip = $("#zipcode").val(); var url = "wunderground.pl?zipcode=" + zip; transform("wunderground.", url); } function transform (xslFileName, url) { // ASIDE: Why won't this work? // xmlhttp.open("GET", "http://api.wunderground.com/api/XXXXX/conditions/forecast/q/21409.xml", false);

// Get the XSLT file var xslhttp = new XMLHttpRequest(); xslhttp.open("GET", xslFileName, false); xslhttp.send('');

// Get the XML input data var xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET", url, false); xmlhttp.send('');

// Transform the XML via the XSLT var processor = new XSLTProcessor(); processor.importStylesheet(xslhttp.responseXML); var newDocument = processor.transformToDocument(xmlhttp.responseXML);

// Replace part of original document with the new content var o = document.getElementById("planet"); var n = newDocument.getElementById("planet"); o.parentNode.replaceChild(n, o); } (Ex 1) wunderground.pl

#!/usr/bin/perl use CGI ":standard"; use strict;

# Need to have "!head" to avoid loading the head function from LWP::Simple. # - The above CGI module also has a head function... use LWP::Simple "!head"; use LWP::UserAgent; use HTTP::Request; use HTTP::Response;

# We want to send XML back print "Content-type: text/xml\n\n";

# Construct URL to get the weather my $zip = param("zipcode"); my $URL = "http://api.wunderground.com/api/XXXX/conditions/forecast/q/$zip.xml";

# Get the XML document and send it back to requestor (the browser) my $contents = get($URL); print $contents; (Ex 1) wunderground.xsl Current weather in: <xsl:value-of select="/response/termsofService"/>

Current weather in:

  • Temperature:
  • Wind:
  • Gusts:
  • Dew Point:
… … SOAP Example 2

• Search flickr.com and show photos on your page. (Ex 2) Sample SOAP request flickr.photos.search value tigers 1

5 83ec7bab1628defd47d893288348fee5

Online API: http://www.flickr.com/services/api/flickr.photos.search.html (Ex 2) Photos XML Data

(Ex 2) photos.html XSLT Example with Web services

Lets now use SOAP to access photos from flickr!

Tags to search for:

This is where the transformed XML in XHTML form will appear.

(Ex 2) flickr.pl (part 1) #!/usr/bin/perl use CGI ":standard";

# Need this to get web pages from Perl use LWP::Simple "!head"; use HTTP::Request; use LWP::UserAgent;

# We want to send XML back print "Content-type: text/xml\n\n"; my $ua = LWP::UserAgent->new(); my $method = "POST"; my $url = "http://api.flickr.com/services/soap/"; my $tags = param("tags"); my $content = " flickr.photos.search value $tags 1 5 83ec7bab1628defd47d893288348fee5 "; (Ex 2) flickr.pl (part 2) use HTTP::Headers; my $header = HTTP::Headers->new(); my $request = HTTP::Request->new($method, $url, $header, $content); use HTTP::Response; my $response = $ua->request($request); if($response->is_success) { # The Flickr response via SOAP is encoded: not recognized right away as XML. # So we need to decode some of the things like < " etc. my $the_response = $response->content; $the_response =~ s/</'<'/eg; # convert < $the_response =~ s/>/'>'/eg; # convert > $the_response =~ s/"/'"'/eg; # convert "

print $the_response; } else { print $response->error_as_HTML; } (Ex 2) flickr.xsl Flickr test

There were results. Here are just some:

http://farm.staticflickr.com//_.jpg

  • (Ex 2) transform2.js function getPhotos() { var tags = $("#tags").val(); var url = "flickr.pl?tags=" + tags; transform("flickr.xsl", url); } function transform (xslFileName, url) { // Get the XSLT file var xslhttp = new XMLHttpRequest(); xslhttp.open("GET", xslFileName, false); xslhttp.send('');

    // Get the XML input data var xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET", url, false); xmlhttp.send('');

    // Transform the XML via the XSLT var processor = new XSLTProcessor(); processor.importStylesheet(xslhttp.responseXML); var newDocument = processor.transformToDocument(xmlhttp.responseXML);

    // Replace part of original document with the new content var o = document.getElementById("planet"); var n = newDocument.getElementById("planet"); o.parentNode.replaceChild(n, o); }